<html>
<head>
<title> UBCmatch - a place to match </title>
</head>

<body>
<center>
<a href="index.htm"><img src="logo.jpg"  border="0"/></a>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<table>
<tr><td><label for="file">ics file:</label></td>
<td><input type="file" name="file" id="file" /></td></tr>
<tr><td><label for="text">Preferred Name:</label></td>
<td><input type="text" name="sender" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>

</body>
</html> 
<?php
include("database.php");
 function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 
function classtime($datetf){

$year= substr($datetf,0,4);
$month= substr($datetf,4,2);
$day= substr($datetf,6,2);
$time=substr($datetf,9,4);
//echo "<br>". $year . " asd" . $month. "asd"  . $day . "asd" . $time. "<br>";
$df= date("D",mktime(0,0,0,(integer)$month,(integer)$day,(integer)$year)); 
if($year==2011)
return $df.$time." term1";
else
return $df.$time." term2";
}

if (findexts($_FILES["file"]["name"]) == "ics" && $_POST['sender']!=="")
  {
  $sender=$_POST['sender'];
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    /*echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
	*/
    if (file_exists("upload/" . $sender . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $sender . $_FILES["file"]["name"]);
      //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      
	  
	  $handle = fopen("upload/". $sender . $_FILES["file"]["name"], "r");
	  
	  while($myline=fgets($handle,4096)){
	  //echo $myline;
	  $mylinetemp=$myline;
	  if(($myline=str_replace("SUMMARY:", "",$myline))!== $mylinetemp){
	  $mylesson=$mylesson.$myline;
	  continue;
	  }
      if(($myline=str_replace("DTSTART;TZID=America/Vancouver:", "",$myline))!== $mylinetemp)
	  {$mylesson=$mylesson.classtime($myline);
	   //echo "<br><br>".$mylesson."<br><br>";
	   $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
	   mysql_select_db($dbname);
	   $sql="SELECT * FROM ubclass WHERE name='$mylesson';";
	   $rep=mysql_query($sql);
	   if($row=mysql_fetch_array($rep))
	   {	
			if(str_replace($sender,"",$row['members'])!== $row['members']){ $mylesson=""; continue; }
			$row['members']=$row['members'].", ".$sender;
			$new=$row['members'];
			$sql="UPDATE ubclass SET members='$new' WHERE name='$mylesson';";
			mysql_query($sql);
	   }
	   else
	   {
			
			$sql="INSERT INTO ubclass SET name='$mylesson' , members='$sender' ;";
			mysql_query($sql);
	   }
	   $mylesson="";
	  }

	  
	  }
	  
	  echo 'update successful , redirecting to results <script>
<!--
window.location = "http://ubcmatch.summerhost.info/main.php"
//-->
</script>';
	  //echo "<br>".fgets($handle, 4096);
	  //echo "<br>".fgets($handle, 4096);
	  //$timetable fread($handle,filesize("upload/". $sender . $_FILES["file"]["name"]));
	  
	  }
    }
  }
else
  {
  echo "Please submit your ics file.";
  }
?> 
 